package org.wby.jwt.utils;

import lombok.NonNull;

import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

public class AESEncryptionUtil {
    private static final String ALGORITHM = "AES";
    private static final String  TRANSFORMATION = "AES";
    private static final String SEC_KEY = "123wby99_R%78hsywh/oO0Ii@Gp8&(b]";

    /**
     * 加密函数
     *
     * @param data
     * @return
     */
    public static String encrypt(@NonNull String data) {
        try {
            // 获取加密对象
            Cipher cipher = Cipher.getInstance(TRANSFORMATION);
            // 创建加密规则
            SecretKeySpec keySpec = new SecretKeySpec(
                    SEC_KEY.getBytes(StandardCharsets.UTF_8),
                    ALGORITHM);
            // 初始化加密模式和算法
            cipher.init(Cipher.ENCRYPT_MODE, keySpec);
            // 加密
            byte[] bytes = cipher.doFinal(data.getBytes(StandardCharsets.UTF_8));
            // 输出加密后的数据
            return Base64.getEncoder().encodeToString(bytes);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        } catch (NoSuchPaddingException e) {
            throw new RuntimeException(e);
        } catch (IllegalBlockSizeException e) {
            throw new RuntimeException(e);
        } catch (BadPaddingException e) {
            throw new RuntimeException(e);
        } catch (InvalidKeyException e) {
            throw new RuntimeException(e);
        }
    }


    /**
     * 解密函数
     * @param encryptData
     * @return
     */
    public static String decrypt(@NonNull String encryptData) {
        try {
            //获取加密对象
            Cipher cipher = Cipher.getInstance(TRANSFORMATION);
            // // 指定密钥规则
            SecretKeySpec keySpec = new SecretKeySpec(SEC_KEY.getBytes(StandardCharsets.UTF_8), ALGORITHM);
            // 初始化加密模式和算法
            cipher.init(Cipher.DECRYPT_MODE, keySpec);
            //解密，上面使用的base64编码，下面直接用密文
            byte[] decode = Base64.getDecoder().decode(encryptData);
            byte[] originalData = cipher.doFinal(decode);
            return new String(originalData);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        } catch (NoSuchPaddingException e) {
            throw new RuntimeException(e);
        } catch (InvalidKeyException e) {
            throw new RuntimeException(e);
        } catch (IllegalBlockSizeException e) {
            throw new RuntimeException(e);
        } catch (BadPaddingException e) {
            throw new RuntimeException(e);
        }


    }

    private static SecretKey generateKey() throws NoSuchAlgorithmException {
        KeyGenerator generator = KeyGenerator.getInstance(ALGORITHM);
        generator.init(128);
        return generator.generateKey();
    }


    public static void main(String[] args) {
        String encrypt = encrypt("wby");
        String decrypt = decrypt(encrypt);
        System.out.println(encrypt);
        System.out.println(decrypt);
    }
}
